#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append('src')

from processors.docx_processor import DOCXProcessor
from core.chunking_engine import ChunkingEngine
from core.text_cleaner import TextCleaner

def demo_question_chunking():
    """演示题目结构切片功能"""
    
    print("=== 题目结构切片功能演示 ===")
    print("\n功能说明：")
    print("- 新增了'题目结构分段'切片策略")
    print("- 专门用于处理选择题、判断题等题目格式")
    print("- 能够自动识别题号并实现每题一个切片")
    print("- 支持自定义题目识别模式")
    
    # 测试文件列表
    test_files = [
        'data/1753880275116-判断题.docx',
        'data/原始数据---选择题.docx'
    ]
    
    for file_path in test_files:
        if not os.path.exists(file_path):
            print(f"\n⚠️  文件不存在: {file_path}")
            continue
            
        print(f"\n=== 测试文件: {os.path.basename(file_path)} ===")
        
        try:
            # 配置
            cleaning_config = {
                'clean_unicode': True,
                'remove_special_chars': False,  # 保留题目中的特殊字符
                'fix_line_breaks': True,
                'remove_headers_footers': False,
                'remove_duplicates': False,
                'remove_empty_lines': True,
                'min_line_length': 1,
                'preserve_formatting': True
            }
            
            chunking_config = {
                'strategy': '题目结构分段',
                'question_pattern': r'^\d+\.',
                'include_question_number': True,
                'merge_sub_questions': False,
                'max_question_size': 2000
            }
            
            # 提取文档内容
            docx_processor = DOCXProcessor()
            with open(file_path, 'rb') as f:
                file_content = f.read()
            
            # 提取文本
            extraction_result = docx_processor.extract_content(file_content)
            raw_text = extraction_result['text']
            
            # 文本清洗
            text_cleaner = TextCleaner(cleaning_config)
            cleaned_text = text_cleaner.clean_text(raw_text)
            
            # 文档切片
            chunking_engine = ChunkingEngine(chunking_config)
            chunks = chunking_engine.chunk_document(cleaned_text)
            
            if chunks:
                # 构建结果
                metadata = {
                    'original_length': len(raw_text),
                    'cleaned_length': len(cleaned_text),
                    'compression_ratio': len(cleaned_text) / len(raw_text) if raw_text else 0
                }
                
                print(f"✅ 处理成功")
                print(f"📊 统计信息:")
                print(f"   - 原始长度: {metadata['original_length']} 字符")
                print(f"   - 清洗后长度: {metadata['cleaned_length']} 字符")
                print(f"   - 压缩比: {metadata['compression_ratio']:.2%}")
                print(f"   - 切片数量: {len(chunks)} 个")
                
                # 分析切片类型
                question_chunks = [c for c in chunks if c['metadata'].get('chunk_type') == 'question']
                header_chunks = [c for c in chunks if c['metadata'].get('chunk_type') == 'header']
                
                print(f"   - 题目切片: {len(question_chunks)} 个")
                print(f"   - 标题切片: {len(header_chunks)} 个")
                
                if question_chunks:
                    avg_length = sum(c['metadata']['char_count'] for c in question_chunks) / len(question_chunks)
                    print(f"   - 平均题目长度: {avg_length:.1f} 字符")
                    
                    # 检查答案完整性
                    with_answers = [c for c in question_chunks if c['metadata'].get('has_answer', False)]
                    print(f"   - 包含答案的题目: {len(with_answers)}/{len(question_chunks)}")
                
                # 显示前3个题目切片示例
                print(f"\n📝 切片示例:")
                for i, chunk in enumerate(question_chunks[:3]):
                    print(f"\n   题目 {i+1}:")
                    print(f"   ID: {chunk['chunk_id']}")
                    print(f"   题号: {chunk['metadata'].get('question_number', 'N/A')}")
                    print(f"   长度: {chunk['metadata']['char_count']} 字符")
                    content_preview = chunk['content'][:100].replace('\n', ' ')
                    print(f"   内容: {content_preview}...")
                
            else:
                print(f"❌ 处理失败")
                
        except Exception as e:
            print(f"❌ 处理出错: {e}")
            import traceback
            traceback.print_exc()
    
    print(f"\n=== 功能特点总结 ===")
    print("✅ 自动识别题目结构（数字+点格式）")
    print("✅ 每题生成独立切片")
    print("✅ 保留题目编号和答案")
    print("✅ 支持判断题和选择题格式")
    print("✅ 可配置题目识别模式")
    print("✅ 在配置页面可选择'题目结构分段'策略")
    
    print(f"\n=== 使用方法 ===")
    print("1. 在配置页面选择'题目结构分段'策略")
    print("2. 配置题目识别模式（默认: ^\\d+\\.）")
    print("3. 上传包含题目的文档文件")
    print("4. 系统自动按题目结构进行切片")
    print("5. 每道题生成一个独立的切片")
    
    print(f"\n🎉 题目结构切片功能演示完成！")

if __name__ == '__main__':
    demo_question_chunking()